package com.woniu.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWTUtil;
import com.woniu.admin.mapper.TUserMapper;
import com.woniu.admin.pojo.TUser;
import com.woniu.admin.service.UserService;
import com.woniu.admin.util.UserException;
import com.woniu.admin.vo.UserDto;
import com.woniu.admin.util.VerifyCodeException;
import com.woniu.admin.vo.TUserVo;
import com.woniu.common.constants.Constants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private TUserMapper userMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;   //redisTemplate是springboot提供的，用来操作redis的

    @Override
    public List<TUser> fineUserList(TUserVo userVo) {
        //先查缓存
        List<TUser> list = (List<TUser>) redisTemplate.opsForValue().get(Constants.USER_LIST_KEY);
        if (!Objects.isNull(list)) {
            return list;
        }
        //查数据库
        List<TUser> userlist = userMapper.userlist(userVo);
        //将数据存入缓存
        redisTemplate.opsForValue().set(Constants.USER_LIST_KEY, userlist);
        return userlist;
    }

//    @Override
//    public TUser login(TUser user) {
//        return userMapper.login(user);
//    }

    @Override
    public List<TUser> selectOneUser(TUser username) {
        return userMapper.selectOneUser(username);
    }

    @Override
    public void delete(Integer[] ids) {
        userMapper.deletes(ids);
    }

    @Override
    public void add(TUser user) {
        userMapper.insertUser(user);
    }

    @Override
    public void update(TUser user) {
        userMapper.updateUser(user);
    }

    //    ---------------jwt-------------------
    @Override
    public UserDto login(TUserVo userVo) {
        //1. 校验验证码
        String verifyCode = (String) redisTemplate.opsForValue().get(userVo.getUuid());
        Boolean isRight = verifyCode.equals(userVo.getCode()) ? true : false;
        if (!isRight) throw new VerifyCodeException("验证码不正确");

        //2. 查询数据库
        TUser example = new TUser();
        example.setUsername(userVo.getUsername());
        List<TUser> list = userMapper.userlist(example);

        if (CollectionUtils.isEmpty(list)) {
            throw new UserException("用户名不存在");
        }
        TUser login = list.get(0);
        if (!login.getPassword().equals(userVo.getPassword())) {
            throw new UserException("用户名和密码错误");
        }

        Map<String, Object> map = BeanUtil.beanToMap(login);
        String token = JWTUtil.createToken(map, login.getUsername().getBytes());

        //3. 存储一份登陆数据到redis
        redisTemplate.opsForValue()
                .set(Constants.LOGIN_PREFIX + login.getUsername(), token, 60, TimeUnit.SECONDS);

        //4. 拷贝
        UserDto userDto = new UserDto();
        BeanUtil.copyProperties(login, userDto);
        userDto.setToken(token);
        return userDto;
    }
}
